package com.abc.pay.client;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

public final class Base64Code {
    private static final byte[] DECODE_TABLE = new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 0, 0, 0, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 0, 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0, 0, 0, 0, 0};
    private static final byte[] ENCODE_TABLE = new byte[]{65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47};

    public Base64Code() {
    }

    public static final byte[] base64Encode(byte[] byteData) {
        if (byteData == null) {
            return null;
        } else {
            byte[] byteDest = new byte[(byteData.length + 2) / 3 * 4];
            int iSrcIdx = 0;
            int iDestIdx;
            for(iDestIdx = 0; iSrcIdx < byteData.length - 2; iSrcIdx += 3) {
                byteDest[iDestIdx++] = (byte)(byteData[iSrcIdx] >>> 2 & 63);
                byteDest[iDestIdx++] = (byte)(byteData[iSrcIdx + 1] >>> 4 & 15 | byteData[iSrcIdx] << 4 & 63);
                byteDest[iDestIdx++] = (byte)(byteData[iSrcIdx + 2] >>> 6 & 3 | byteData[iSrcIdx + 1] << 2 & 63);
                byteDest[iDestIdx++] = (byte)(byteData[iSrcIdx + 2] & 63);
            }
            if (iSrcIdx < byteData.length) {
                byteDest[iDestIdx++] = (byte)(byteData[iSrcIdx] >>> 2 & 63);
                if (iSrcIdx < byteData.length - 1) {
                    byteDest[iDestIdx++] = (byte)(byteData[iSrcIdx + 1] >>> 4 & 15 | byteData[iSrcIdx] << 4 & 63);
                    byteDest[iDestIdx++] = (byte)(byteData[iSrcIdx + 1] << 2 & 63);
                } else {
                    byteDest[iDestIdx++] = (byte)(byteData[iSrcIdx] << 4 & 63);
                }
            }
            for(iSrcIdx = 0; iSrcIdx < iDestIdx; ++iSrcIdx) {
                if (byteDest[iSrcIdx] < 26) {
                    byteDest[iSrcIdx] = (byte)(byteDest[iSrcIdx] + 65);
                } else if (byteDest[iSrcIdx] < 52) {
                    byteDest[iSrcIdx] = (byte)(byteDest[iSrcIdx] + 97 - 26);
                } else if (byteDest[iSrcIdx] < 62) {
                    byteDest[iSrcIdx] = (byte)(byteDest[iSrcIdx] + 48 - 52);
                } else if (byteDest[iSrcIdx] < 63) {
                    byteDest[iSrcIdx] = 43;
                } else {
                    byteDest[iSrcIdx] = 47;
                }
            }
            while(iSrcIdx < byteDest.length) {
                byteDest[iSrcIdx] = 61;
                ++iSrcIdx;
            }
            return byteDest;
        }
    }

    public static final String base64Encode(String strInput) {
        return strInput == null ? null : base64Encode(strInput, "GBK");
    }

    public static final String base64Encode(String strInput, String charSet) {
        if (strInput == null) {
            return null;
        } else {
            String strOutput = null;
            byte[] byteData = new byte[strInput.length()];
            try {
                byteData = strInput.getBytes(charSet);
                strOutput = new String(base64Encode(byteData), charSet);
                return strOutput;
            } catch (UnsupportedEncodingException var5) {
                return null;
            }
        }
    }

    public static final String base64Encode(InputStream in, String charSet) {
        try {
            byte[] buff = new byte[1024];
            ByteArrayOutputStream out = new ByteArrayOutputStream(2048);
            int c;
            while((c = in.read(buff, 0, 1024)) != -1) {
                out.write(buff, 0, c);
            }
            in.close();
            out.flush();
            byte[] tmp2 = base64Encode(out.toByteArray());
            out.close();
            return new String(tmp2, charSet);
        } catch (IOException var6) {
            return "";
        }
    }

    public static byte[] base64Decode(byte[] base64Data) {
        if (base64Data == null) {
            return null;
        } else if (base64Data.length == 0) {
            return new byte[0];
        } else if (base64Data.length % 4 != 0) {
            throw new IllegalArgumentException("数据不完整，长度为：" + base64Data.length);
        } else {
            int groupCount = base64Data.length / 4;
            int lastData = base64Data.length;
            while(base64Data[lastData - 1] == 61) {
                --lastData;
                if (lastData == 0) {
                    return new byte[0];
                }
            }
            byte[] result = new byte[lastData - groupCount];
            int resultIndex = 0;
            int dataIndex;
            int temp;
            for(dataIndex = 0; dataIndex + 4 < base64Data.length; result[resultIndex++] = (byte)(temp & 255)) {
                temp = DECODE_TABLE[base64Data[dataIndex++]];
                temp = (temp << 6) + DECODE_TABLE[base64Data[dataIndex++]];
                temp = (temp << 6) + DECODE_TABLE[base64Data[dataIndex++]];
                temp = (temp << 6) + DECODE_TABLE[base64Data[dataIndex++]];
                result[resultIndex++] = (byte)(temp >> 16 & 255);
                result[resultIndex++] = (byte)(temp >> 8 & 255);
            }
            temp = 0;
            int j;
            for(j = 0; dataIndex < base64Data.length; ++j) {
                temp = (temp << 6) + DECODE_TABLE[base64Data[dataIndex]];
                ++dataIndex;
            }
            while(j < 4) {
                temp <<= 6;
                ++j;
            }
            result[resultIndex++] = (byte)(temp >> 16 & 255);
            if (base64Data[dataIndex - 2] != 61) {
                result[resultIndex++] = (byte)(temp >> 8 & 255);
            }
            if (base64Data[dataIndex - 1] != 61) {
                result[resultIndex++] = (byte)(temp & 255);
            }
            return result;
        }
    }

    public static final String base64Decode(String strInput) {
        return strInput == null ? null : base64Decode(strInput, "GBK");
    }

    public static final String base64Decode(String strInput, String charSet) {
        if (strInput == null) {
            return null;
        } else {
            String strOutput = null;
            byte[] byteData = new byte[strInput.length()];
            try {
                byteData = strInput.getBytes(charSet);
                strOutput = new String(base64Decode(byteData), charSet);
                return strOutput;
            } catch (UnsupportedEncodingException var5) {
                return null;
            }
        }
    }

    public static final String base64Decode(InputStream in, String charSet) {
        try {
            byte[] buff = new byte[1024];
            ByteArrayOutputStream out = new ByteArrayOutputStream(2048);
            int c;
            while((c = in.read(buff, 0, 1024)) != -1) {
                out.write(buff, 0, c);
            }
            in.close();
            out.flush();
            byte[] tmp2 = base64Decode(out.toByteArray());
            out.close();
            return new String(tmp2, charSet);
        } catch (IOException var6) {
            return "";
        }
    }

    public static String Encode(String para) {
        return base64Encode(para);
    }

    public static String Decode64(String para) {
        String retStr;
        try {
            retStr = base64Decode(para);
        } catch (Exception var4) {
            retStr = "ERROR";
            var4.printStackTrace();
        }
        return retStr;
    }

    static {
        int index = 0;
        int i;
        for(i = 65; i <= 90; i = (char)(i + 1)) {
            ENCODE_TABLE[index++] = (byte)i;
        }
        for(i = 97; i <= 122; i = (char)(i + 1)) {
            ENCODE_TABLE[index++] = (byte)i;
        }
        for(i = 48; i <= 57; i = (char)(i + 1)) {
            ENCODE_TABLE[index++] = (byte)i;
        }
        ENCODE_TABLE[index++] = 43;
        ENCODE_TABLE[index++] = 47;
        for(i = 0; i < 64; ++i) {
            DECODE_TABLE[ENCODE_TABLE[i]] = (byte)i;
        }
    }

}

